#!/usr/bin/env node

import { packageVersion, repositoryUrl } from '@/config'
import { program } from 'commander'
import x from 'conf'

new x({})
import {
  highlight,
  logHighlight,
  logInfo,
  warning,
  info,
  success,
} from '@/utils'
import { RepositoryService } from '@/api'
import jenkinsAction from '@/actions/jenkins'

program.name('w-cli').description('w-cli-toolbox')
program.hook('preAction', () => {
  checkNewVersion()
})

program
  .command('jenkins')
  .aliases(['jk'])
  .description('jenkins相关命令')
  // .option("-r, --risky", "跳过分支检查, 可能触发意外的构建任务")
  .option('-c, --create', '创建配置')
  .action(jenkinsAction)

program.parse(process.argv)

async function checkNewVersion() {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const packageJson: any = await RepositoryService.getRepositoryFile(
    717,
    'package.json',
    'master',
  )
  console.warn(111)
  const latestVersion = packageJson.version
  logInfo(`当前版本: v${packageVersion}`)
  if (latestVersion && latestVersion !== packageVersion) {
    logHighlight(`最新版本: v${latestVersion}`)
    console.log(
      info(
        `检测到新版本 ${warning(latestVersion)}, 请执行 ${success(
          `npm i -g git+${repositoryUrl}`,
        )} 更新版本`,
      ),
    )
  }
}
